/*
  Name: DeadProxyPacketBuffer.h
  Copyright:
  Author: Carl Chatfield
  Date: 10/01/08 12:55
  Description: Class used for analyzing a packet. Note, there is no bounds checking to maximize efficiency
*/

#ifndef __DEADPROXYPACKETBUFFER_h__
#define __DEADPROXYPACKETBUFFER_h__

#ifdef __BORLANDC__
	#pragma hdrstop
#endif

#ifndef WX_PRECOMP
	#include <wx/wx.h>
#else
	#include <wx/wxprec.h>
#endif

#include "DeadProxyXTEA.h"

#include "DeadProxyDefinitions.h"

struct CharacterLoginInfo
{
    char * name;
    char * world;
    int IP;
    short port;
};

class CharacterList
{
    public:
        CharacterLoginInfo CharList[20];
        int length;
        //virtual ~CharacterList();
};

class PacketBuffer
{
    private:
        unsigned char * buffer;
        unsigned int pos;
        int len;
        TibiaXTEA * Crypt;
    public:
        PacketBuffer();
        void SetBuffer(unsigned char * buffer, int length);
        void NewBuffer(int length);
        void SetKey(unsigned char * key);
        int GetLength();
        int GetPos();
        void SetPos(int p);
        unsigned char * GetCopy();
        virtual ~PacketBuffer();
        unsigned char * GetBuffer();
        void Encrypt();
        void Decrypt();
        //Read functions read from the buffer and move pos forward
        unsigned char ReadChar();
        unsigned short ReadShort();
        unsigned int ReadInt();
        unsigned long ReadLong();
        unsigned char * ReadCharArray(int length);
        char * ReadString();
        Position ReadPos();
        //Peek functions read from the buffer, but to not move pos forwards
        unsigned char PeekChar();
        unsigned short PeekShort();
        unsigned int PeekInt();
        unsigned long PeekLong();
        unsigned char * PeekCharArray(int length);
        char * PeekString();
        Position PeekPos();
        //Unread functions write to the buffer and move pos backwards
        void UnreadChar(unsigned char write);
        void UnreadShort(unsigned short write);
        void UnreadInt(unsigned int write);
        void UnreadLong(unsigned long write);
        void UnreadCharArray(unsigned char * write, int length);
        // Unpeek functions write to the buffer, but to not move pos backwards
        void UnpeekChar(unsigned char write);
        void UnpeekShort(unsigned short write);
        void UnpeekInt(unsigned int write);
        void UnpeekLong(unsigned long write);
        void UnpeekCharArray(unsigned char * write, int length);
        // Write functions write data to the buffer and move pos forwards
        void WriteChar(unsigned char write);
        void WriteShort(unsigned short write);
        void WriteInt(unsigned int write);
        void WriteLong(unsigned long write);
        void WriteCharArray(unsigned char * write, int length);
        // Skip functions move pos forward but do not read or write
        void SkipChar();
        void SkipShort();
        void SkipInt();
        void SkipLong();
        void Skip(int length);
        void SkipString();
        // Skip functions move pos backwards but do not read or write
        void UnskipChar();
        void UnskipShort();
        void UnskipInt();
        void UnskipLong();
        void Unskip(int length);
        // Parsing Functions
        int ParseLogin(CharacterList ** CharList);
};

#endif
